<?php
/**
 * @author 
 * @copyright 2014
 */
 
class incModelMenu extends CI_Model{
	function __construct(){
		parent::__construct('menus');
	}
    public function getList($groupid,$uris){
		$sqlp = "SELECT g.id, g.params
				FROM groups g
				WHERE g.isdelete = 0
				and g.id = '$groupid'
				";	
		$query = $this->model->query($sqlp)->execute();
		if(empty($query[0]->params)){
			return "";
		}
		$permission = json_decode($query[0]->params);
		$right = "";
		if(count($permission) == 0){
			return "";
		}
		foreach($permission as $key=>$val){
			$right.= ",".$key;
		}
		$right = substr($right,1);
		
		$sql    =   " SELECT m.name, m.parent, m.route, m.id, m.classicon
                        FROM menus m                      
                        WHERE m.parent = 0
						and m.isdelete = 0 
						order by m.ordering ASC";
		$result =  $this->model->query($sql)->execute(); 
		#region menu
		$menu = '';
		foreach($result as $item){
			$asub = $this->getChildren($item->id,$right);
			if(empty($item->classicon)){
				$classicon = 'fa-folder-o';
			}
			else{
				$classicon = $item->classicon;
			}
			$active = '';
            if($asub){
				$menu.='<li  '.$active.'><a href="javascript:;"><i class="fa '.$classicon.'"></i>
    					<span class="title">'.$item->name .'</span><span class="arrow "></span></a>';
                $menu.=' <ul class="sub-menu">';
                $menu.=$asub;
                $menu.='</ul></li>';   
			}
			else{
				//$link = base_url().$item->route .'.html';
				//$menu.="<li $active><a href='$link'><i class='fa ".$classicon."'></i><span class='title'>".$item->name."</span></a></li>";  
			}
		}
		return $menu;	
		#end
	}
    private function getChildren($id, $right){
		$sql =  " SELECT m.name, m.parent, m.route, m.id, m.classicon
                        FROM menus m                      
                        WHERE m.parent = $id
						and m.id in ($right)
						and m.isdelete = 0
						order by m.ordering ASC";
		$children =  $this->model->query($sql)->execute();
		$menu = '';
		foreach($children as $item){
			 $asub = $this->getChildren($item->id,$right);   
			 if(empty($item->classicon)){
				$classicon = 'fa-folder-o';
			 }
			 else{
				$classicon = $item->classicon;
			 }
			 $active = '';
			 if($asub){
				$menu.="<li $active> <a href='javascript:;'>
                            <i class='fa $classicon'></i><span class='title'>".$item->name."</span><span class='arrow '></span></a>";
				$menu.='<ul class="sub-menu">';
				$menu.=$asub;
				$menu.='</ul></li>';  
			 }
			 else{
				$link = base_url().$item->route .'.html';
				$menu.= "<li $active><a href='".$link."'><i class='fa $classicon'></i><span class='title'>".$item->name."</span></a></li>"; 
			 }
		}
		return $menu;
    }
}